package com.example.bookmall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookmall.entity.address.Address;
import com.example.bookmall.entity.Result;
import com.example.bookmall.mapper.AddressMapper;
import com.example.bookmall.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    //设置默认地址的时候 或者添加为默认地址的时候 将该用户所有地址设为非默认
    @Override
    @Transactional
    public boolean setDefaultAddress(Integer addressId, Integer userId) {
        // 先将该用户的所有地址设为非默认
        lambdaUpdate()
                .eq(Address::getUserId, userId)
                .set(Address::getIsDefault, 0)
                .update();

        // 再将指定地址设为默认
        return lambdaUpdate()
                .eq(Address::getAddressId, addressId)
                .eq(Address::getUserId, userId)
                .set(Address::getIsDefault, 1)
                .update();
    }

    //添加地址
    @Override
    public Result addAddress(Address address) {
        //TODO：地址集成

        if(!StringUtils.hasText(address.getConsignee())){
            return Result.error("请填写收货人姓名");
        }
        if(!StringUtils.hasText(address.getPhone())){
            return Result.error("请填写收货人手机号");
        }
        if(!StringUtils.hasText(address.getProvince())){
            return Result.error("请填写省");
        }
        if(!StringUtils.hasText(address.getCity())){
            return Result.error("请填写市");
        }
        if(!StringUtils.hasText(address.getDistrict())){
            return Result.error("请填写县");
        }
        if(!StringUtils.hasText(address.getDetail())){
            return Result.error("请填写详细地址");
        }

        addressMapper.insert(address);

        //  如果设置为默认地址，先将其他地址设为非默认
        if (address.getIsDefault() != null && address.getIsDefault()==1) {
            setDefaultAddress(address.getAddressId(), address.getUserId());
        }

        return Result.success("地址添加成功");
    }

    //查询地址列表
    @Override
    public List<Address> addressList(Integer userId) {
        return addressMapper.selectList(
                new LambdaQueryWrapper<Address>()
                        .eq(Address::getUserId, userId)
                        .eq(Address::getDeleted, 0)
        );
    }

    //修改地址
    @Override
    public Result modifyAddress(Integer addressId,Integer userId,Address address) {
        if(!StringUtils.hasText(address.getConsignee())){
            return Result.error("请填写收货人姓名");
        }
        if(!StringUtils.hasText(address.getPhone())){
            return Result.error("请填写收货人手机号");
        }
        if(!StringUtils.hasText(address.getProvince())){
            return Result.error("请填写省");
        }

        if(!StringUtils.hasText(address.getCity())){
            return Result.error("请填写市");
        }
        if(!StringUtils.hasText(address.getDistrict())){
            return Result.error("请填写县");
        }
        if(!StringUtils.hasText(address.getDetail())){
            return Result.error("请填写详细地址");
        }
        address.setUserId(userId);
        address.setAddressId(addressId);

        //  如果设置为默认地址，先将其他地址设为非默认
        if(address.getIsDefault() != null && address.getIsDefault()==1){
            setDefaultAddress(address.getAddressId(), address.getUserId());
        }
        addressMapper.updateById(address);
        return Result.success();
    }

    //删除地址
    @Override
    public Result removeById(Integer addressId) {
        if(addressMapper.selectById(addressId)==null){
            return Result.error("没有该地址");
        }
        Address address = getById(addressId);

        address.setDeleted(1);
        addressMapper.updateById(address);

        return Result.success("删除成功");
    }

    @Override
    public Address getById(Integer addressId) {
        return addressMapper.selectById(addressId);
    }

    @Override
    public Address getByAddressIdUserId(Integer newAddressId, Integer userId) {

        return addressMapper.selectOne(
                new LambdaQueryWrapper<Address>()
                        .eq(Address::getAddressId, newAddressId)
                        .eq(Address::getUserId, userId)
        );
    }

    //取消设为默认
    @Override
    public boolean setUnDefaultAddress(Integer addressId, Integer userId) {

        //将指定地址设为非默认
        return lambdaUpdate()
                .eq(Address::getAddressId, addressId)
                .eq(Address::getUserId, userId)
                .set(Address::getIsDefault, 0)
                .update();

    }

    @Override
    // 在AddressService中添加新方法
    public Address getByIdIncludeDeleted(Integer id) {


      /*  addressMapper.selectList(

        );

        *//*LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getAddressId, id)
                .eq(Address::getDeleted, 1); // 强制查询已删除的地址*/
        Address address = addressMapper.selectOne(new LambdaQueryWrapper<Address>()
                .eq(Address::getAddressId, id).eq(Address::getDeleted, 0));
        System.out.println(address);
        if(address==null){
            address = addressMapper.selectOne(new LambdaQueryWrapper<Address>()
                    .eq(Address::getAddressId, id).eq(Address::getDeleted, 1));
        }
        System.out.println(address);
        return address;

//                .eq(Address::getDeleted, 1));
    }

}
